package com.lc.hongtai.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;

import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonRegisterXieyi;
import com.lc.hongtai.toast.LovelyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by Administrator on 2017/6/6.
 */
public class RegisterXieyiActivity extends BaseActivity implements ImageView.OnClickListener {
    @BoundView(R.id.regist_web_wv)
    WebView regist_web_wv;

    @BoundView(R.id.iv_back)
    ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_xieyi);
        WebSettings webSettings = regist_web_wv.getSettings();
        iv_back.setOnClickListener(this);
//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
// 若加载的 html 里有JS 在执行动画等操作，会造成资源浪费（CPU、电量）
// 在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可

//支持插件


//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

      /*  if (TextUtils.isEmpty(getIntent().getStringExtra("url"))) {
            Toast.makeText(context, "传过来的是空值", Toast.LENGTH_SHORT).show();
        } else {
            regist_web_wv.loadUrl(getIntent().getStringExtra("url"));
        }*/

        getAccessToken();
    }

    private void getAccessToken() {
        new JsonAccessToken("index/dy", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        //   wv01.loadUrl(getIntent().getStringExtra("url") + "&access_token=" + info.access_token);
                        initData(info.access_token);
//                        regist_web_wv.loadUrl("http://vhost906.zihaistar.com/api/index/dy?"+"access_token"+info.access_token);
                        Log.d("http://vhost906.zihaistar.com/api/index/dy?" + "access_token" + info.access_token, "22222222222222");
                        regist_web_wv.loadUrl("http://vhost906.zihaistar.com/api/index/dy?" + "access_token" + info.access_token);


                    } else {
                        //UtilToast.show(AdvertisingActivity.this, "安全验证失败！");
                        LovelyToast.makeText(RegisterXieyiActivity.this, "安全验证失败!", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  UtilToast.show(AdvertisingActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterXieyiActivity.this, "安全验证失败!", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);

            }
        }).execute(this, true);

    }

    private void initData(String access_token) {
        new JsonRegisterXieyi(access_token, 5, new AsyCallBack<JsonRegisterXieyi.Info>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);

            }

            @Override
            public void onSuccess(String toast, int type, JsonRegisterXieyi.Info info) throws Exception {
                super.onSuccess(toast, type, info);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }
        }).execute(RegisterXieyiActivity.this, false);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
